package com.gmcc.docmanager.control;

import java.util.List;

import com.gmcc.docmanager.app.Constants;
import com.gmcc.docmanager.entity.LibCodeType;
import com.gmcc.docmanager.loader.ApplyAuthorizationResponseParser;
import com.gmcc.docmanager.loader.QueryApplicationResponseParser;
import com.gmcc.docmanager.loader.Request;
import com.gmcc.docmanager.loader.Response;
import com.gmcc.docmanager.loader.ResponseListener;
import com.gmcc.docmanager.loader.Sender;

import android.content.Context;

public class PersonalAffairsControl extends AbstractControl {

	private static PersonalAffairsControl instance;

	private Context mContext;

	private PersonalAffairsControl(Context context) {
		mContext = context;
	}

	public static void init(Context context) {
		instance = new PersonalAffairsControl(context);
	}

	public static PersonalAffairsControl getInstance() {
		return instance;
	}

	public void applyAuthorization(LibCodeType libCodeType, String docID,
			List<String> efileIDs, String reasons,
			final ApplyAuthorizationListener listener) {
		if (!LoginControl.isLogin()) {
			listener.onResponseFail(createNotLoginResponse());
			return;
		}

		Request request = new Request();
		if (Constants.IS_INTERFACE_TEST) {
			request.setRequestUrl(Constants.APPLY_AUTH_URL_TEST);
		} else {
			request.setRequestUrl(Constants.APPLY_AUTH_URL);
		}
		request.addRequestData("libCode", String.valueOf(libCodeType.value()));
		request.addRequestData("docID", docID);
		request.addRequestData("efileIDs", listToJson("efileID", efileIDs));
		request.addRequestData("reasons", String.valueOf(reasons));

		ApplyAuthorizationResponseParser responseParser = new ApplyAuthorizationResponseParser(
				new ResponseListener() {
					@Override
					public void onResponse(Response response) {
						if (response.getCode() != 0) {
							listener.onResponseFail(response);
						} else {
							listener.onResponseSuccess(response);
						}
					}
				});

		Sender sender = new Sender(mContext, request, responseParser);
		sender.send();
	}

	private String listToJson(String tag, List<String> listData) {
		if (listData != null && !listData.isEmpty()) {
			StringBuffer stringBuffer = new StringBuffer("{[");
			for (String data : listData) {
				if (stringBuffer.length() > 2) {
					stringBuffer.append(",");
				}
				stringBuffer.append("{").append("\"").append(tag)
						.append("\":\"").append(data).append("\"}");
			}
			stringBuffer.append("]}");
		}
		return "";
	}

	public interface ApplyAuthorizationListener {
		void onResponseSuccess(Response response);

		void onResponseFail(Response response);
	}

	public void queryApplication(int startNum, int pageNum,
			final QueryApplicationListener listener) {
		if (!LoginControl.isLogin()) {
			listener.onResponseFail(new QueryApplicationResponse(
					createNotLoginResponse()));
			return;
		}

		Request request = new Request();
		if (Constants.IS_INTERFACE_TEST) {
			request.setRequestUrl(Constants.QUERY_APPLICATION_URL_TEST);
		} else {
			request.setRequestUrl(Constants.QUERY_APPLICATION_URL_TEST);
		}

		request.addRequestData("bpos", String.valueOf(startNum));
		request.addRequestData("pageNum", String.valueOf(pageNum));

		QueryApplicationResponseParser responseParser = new QueryApplicationResponseParser(
				new ResponseListener() {
					@Override
					public void onResponse(Response response) {
						if (response.getCode() != 0) {
							listener.onResponseFail(new QueryApplicationResponse(
									response));
						} else {
							listener.onResponseSuccess(new QueryApplicationResponse(
									response));
						}
					}
				});

		Sender sender = new Sender(mContext, request, responseParser);
		sender.send();
	}

	public interface QueryApplicationListener {
		void onResponseSuccess(QueryApplicationResponse response);

		void onResponseFail(QueryApplicationResponse response);
	}
}
